﻿using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using FreeSql.DataAnnotations;
using Model;

namespace DAL
{
    public class BuyCart
    {
        public int InsertBuyCart(BuyCartInfo buy)
        {
            IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
            var col = mongodb.GetCollection<BsonDocument>("BuyCartList");
                var doc = new BsonDocument();
                doc["account"] = buy.account;
                col.InsertOne(doc);                      
            return 0;
        }
        //更新用户的购物车
        public int UpdateBuyCart(BuyCartInfo buy, string account)
        {
            try
            {
                //考虑当用户修改act的时候，亦需要更改购物车的act，认定第二个参数为oldact
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var col = mongodb.GetCollection<BsonDocument>("BuyCartList"); //定义获取“Name”值为“xumingxiang”的查询条件
                var query = new QueryDocument { { "account", account } }; //定义更新文档
                var update = new UpdateDocument { { "$set", new QueryDocument { { "account", buy.account }, { "PidList", new BsonArray(buy.PidList) }, { "BnumList", new BsonArray(buy.Bnum) } } } }; //执行更新操作
                col.UpdateOne(query, update);
            }
            catch
            {
                return -1;
            }
            return 0;
        }
        //获取用户购物车信息
        public BuyCartInfo GetBuyCart(string account)
        {
            try
            {               
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                BuyCartInfo buycart = new BuyCartInfo(account);
                var query = new QueryDocument { { "account", account } };
                var col = mongodb.GetCollection<BsonDocument>("BuyCartList");
                var doc = col.Find(query).FirstOrDefault();
                buycart.PidList = new List<string>();
                buycart.Bnum = new List<int>();
                if (doc.Contains("PidList") == true)
                {
                    for (int i = 0; i < doc.GetValue("PidList").AsBsonArray.Count; i++)
                    {
                        string s = doc.GetValue("PidList").AsBsonArray[i].ToString();
                        int a = doc.GetValue("BnumList").AsBsonArray[i].ToInt32();
                        buycart.PidList.Add(s);
                        buycart.Bnum.Add(a);
                    }
                }
                
                return buycart;
            }
            catch
            {
                return null;
            }

        }
        //删除用户的购物车
        public int DeleteBuyCart(string account)
        {
            try
            {
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var col = mongodb.GetCollection<BsonDocument>("BuyCartList");
                var query = new QueryDocument { { "account", account } };
                col.DeleteMany(query);
            }
            catch
            {
                return -1;
            }
            return 0;
        }
        public int UpdBuyAct(string newact, string oldact)
        {
            try
            {
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var col = mongodb.GetCollection<BsonDocument>("BuyCartList");
                var query = new QueryDocument { { "account", oldact } };
                var update = new UpdateDocument { { "$set", new QueryDocument { { "account", newact } } } }; //执行更新操作
                col.UpdateOne(query, update);
            }
            catch
            {
                return -1;
            }
            return 0;
        }
    }
}
